package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.IEmployeeDAO;
import com.twentyOne.ClothingLand.vo.Employee;

public class EmployeeDAO extends BaseDAO implements IEmployeeDAO {

	@Override
	public void addEmployee(Employee employee) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(employee);
		tx.commit();
		session.close();
		
	}

	@Override
	public Employee authenticateEmployee(String userName, String password, int shopId) {
		
		Session session = getSession();
		String hql = "from Employee as e where e.userName='" + userName 
					 + "' and e.password='" + password
					 + "' and e.shopId=" + shopId;
		Query query = session.createQuery(hql);
		List<Employee> list = query.list();
		session.close();
		if (list.size() != 0) {
			Employee employee = list.get(0);
			return employee;
		}else {
			return null;
		}
		
	}

	@Override
	public Employee authenticateAdmin(String userName, String password) {
		
		Session session = getSession();
		String hql = "from Employee as e where e.userName='" + userName + "' and e.password='" + password + "'";
		Query query = session.createQuery(hql);
		List<Employee> list = query.list();
		session.close();
		if (list.size() != 0) {
			Employee employee = list.get(0);
			return employee;
		}else {
			return null;
		}
		
	}

}
